<?php
/**
 * Description of NewsController
 * @author : domain.web.online@gmail.com
 * @since Oct 19, 2012
 */
class FollowController extends AppController {

    public $name = 'Follow';
    public $uses = array('Follow', 'User', 'Thongbao');
    public $type = 'follow';
 
    public function beforeFilter() {
        parent::beforeFilter();
    }

    public function active($id = null){
        $this->checklogin();
        $user_follow = $this->User->findById($id);
        if(empty($user_follow)){
            if(isset($this->notice['not_exits_link'])) $this->Session->setFlash($this->notice['not_exits_link'], 'default', array('class' => 'alert alert-error'));
            $this->redirect($this->referer());
            exit;
        }
        
        //not ket ban vs minh
        if($user_follow['User']['id'] == $this->user_login['id']){
            if(isset($this->notice['follow_double'])) $this->Session->setFlash($this->notice['follow_double'], 'default', array('class' => 'alert alert-error'));
            $this->redirect($this->referer());
            exit;
        }
        //check xem ban va nguoi nay da phai la ban chua
        $check_follow_exits = $this->Follow->find('first', array(
            'conditions' => array(
                'Follow.type' => $this->type,
                'Follow.id_friend' => $id,
                'Follow.id_user' => $this->user_login['id']
            )
        )); 
        if($check_follow_exits){
            if(isset($this->notice['follow_exits'])) $this->Session->setFlash($this->notice['follow_exits'], 'default', array('class' => 'alert alert-error'));
            $this->redirect($this->referer());
            exit;
        }
        
        //xu ly them ban
        $this->Follow->create();
        $data = array();
        $data['Follow']['id_user'] = $this->user_login['id'];
        $data['Follow']['type'] = $this->type;
        $data['Follow']['id_friend'] = $id;
        $data['Follow']['status'] = 1;
        if($this->Follow->save($data['Follow'])){
            
            
            $thongbao['Thongbao']['content']  = 'Bạn '.$user_follow['User']['fullname'].' đang theo dõi bạn.';
            $thongbao['Thongbao']['status'] = 0;
            $thongbao['Thongbao']['type'] = 'mess';
            $thongbao['Thongbao']['user_id'] = $id;
            $thongbao['Thongbao']['user_tuongtac'] = $this->user_login['id'];
            $this->Thongbao->save($thongbao['Thongbao']);
            
            if(isset($this->notice['follow_success'])) $this->Session->setFlash($this->notice['follow_success'], 'default', array('class' => 'alert alert-success'));
            $this->redirect($this->referer());
            exit;
        }else{
            if(isset($this->notice['follow_err'])) $this->Session->setFlash($this->notice['follow_err'], 'default', array('class' => 'alert alert-error'));
            $this->redirect($this->referer());
            exit;
        }
        $this->redirect($this->referer());
        exit();
    }
    
    public function close($id = null){
        $this->checklogin();
        $user_follow = $this->User->findById($id);
        if(empty($user_follow)){
            if(isset($this->notice['not_exits_link'])) $this->Session->setFlash($this->notice['not_exits_link'], 'default', array('class' => 'alert alert-error'));
            $this->redirect($this->referer());
            exit;
        }
        
        //not ket ban vs minh
        if($user_follow['User']['id'] == $this->user_login['id']){
            if(isset($this->notice['follow_delete_double'])) $this->Session->setFlash($this->notice['follow_delete_double'], 'default', array('class' => 'alert alert-error'));
            $this->redirect($this->referer());
            exit;
        }
        //check xem ban va nguoi nay da phai la ban chua
        $check_follow_exits = $this->Follow->find('first', array(
            'conditions' => array(
                'Follow.type' => $this->type,
                'Follow.id_friend' => $id,
                'Follow.id_user' => $this->user_login['id']
            )
        )); 
        if(!$check_follow_exits){
            if(isset($this->notice['follow_delete_not_extis'])) $this->Session->setFlash($this->notice['follow_delete_not_extis'], 'default', array('class' => 'alert alert-error'));
            $this->redirect($this->referer());
            exit;
        }
        
        //xu ly xoa ket ban
        if($this->Follow->delete($check_follow_exits['Follow']['id'])){
            
            $bb = $this->User->findById($this->user_login['id']); 
            
            $thongbao['Thongbao']['content']  = 'Bạn '.$bb['User']['fullname'].' đã hủy theo dõi bạn.';
            $thongbao['Thongbao']['status'] = 0;
            $thongbao['Thongbao']['type'] = 'mess';
            $thongbao['Thongbao']['user_id'] = $id;
            $thongbao['Thongbao']['user_tuongtac'] = $this->user_login['id'];
            $this->Thongbao->save($thongbao['Thongbao']);
            
            if(isset($this->notice['follow_delete'])) $this->Session->setFlash($this->notice['follow_delete'], 'default', array('class' => 'alert alert-success'));
            $this->redirect($this->referer());
            exit;
        }else{
            if(isset($this->notice['follow_delete_err'])) $this->Session->setFlash($this->notice['follow_delete_err'], 'default', array('class' => 'alert alert-error'));
            $this->redirect($this->referer());
            exit;
        }
        $this->redirect($this->referer());
        exit();
    }
    
}